package com.yuanli.cosmeticmirror.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.kongzue.dialog.v3.TipDialog;
import com.kongzue.dialog.v3.WaitDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * @author CYH06
 */
public abstract class BaseFragment extends Fragment {


    public Context mContext;
    private Unbinder unbinder;
    private TipDialog dialog;

    //当前view状态变化
    private boolean beforeVisibleState = false;

    /**
     * View点击
     **/
    protected abstract int getContentView();


    /**
     * 初始化
     */
    protected abstract void init(Bundle paramBundle);

    /**
     * 初始化控件
     */
    protected abstract void initView(View view);

//    protected abstract void initData();

    // 实现具体的数据请求逻辑
//    protected abstract void loadData();

    public BaseFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(getContentView(), container, false);
        unbinder = ButterKnife.bind(this, view);
        init(savedInstanceState);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
//        initData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!beforeVisibleState && isResumed()) {
            dispatchHintState(true);
        }
        beforeVisibleState = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (beforeVisibleState && !isResumed()) {
            dispatchHintState(false);
        }
        beforeVisibleState = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    private void dispatchHintState(boolean state) {
        //避免走两次，因为onResume会再次调用
        if (beforeVisibleState == state) {
            return;
        }
        beforeVisibleState = state;
        if (state) {
            startLoadData();
        } else {
            stopLoadData();
        }
    }

    /**
     * 停止加载数据
     */
    protected abstract void stopLoadData();

    /**
     * 开始加载数据
     */
    protected abstract void startLoadData();

    public void showToast(String msg) {
        Toast.makeText(MyApp.getInstance(), msg, Toast.LENGTH_SHORT).show();
    }

    public void dissmis() {
        if (dialog != null && dialog.isShow) {
            dialog.doDismiss();
        }
    }

    public void showLoadDialog(String msg) {
        dialog = null;
        dialog = WaitDialog.show((AppCompatActivity) mContext, msg);

    }

    /**
     * 正在加载。。。
     */
    public void showLoadDialog() {
        dialog = null;
        dialog = WaitDialog.show((AppCompatActivity) mContext, "正在加载...");
    }

    public void dialogDismiss() {
        if (dialog != null && dialog.isShow) {
            dialog.doDismiss();
        }
    }

}